package cn.edu.qzuie.artist.module.td.service.submitactivity;

import cn.edu.qzuie.artist.module.td.dal.dataobject.activity.ActivityDO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.student.StudentDO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.work.WorkDO;
import cn.edu.qzuie.artist.module.td.dal.mysql.activity.ActivityMapper;
import cn.edu.qzuie.artist.module.td.dal.mysql.student.StudentMapper;
import cn.edu.qzuie.artist.module.td.dal.mysql.work.WorkMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.edu.qzuie.artist.module.td.controller.admin.submitactivity.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.submitactivity.SubmitActivityDO;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;

import cn.edu.qzuie.artist.module.td.dal.mysql.submitactivity.SubmitActivityMapper;

import static cn.edu.qzuie.artist.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.edu.qzuie.artist.module.td.enums.ErrorCodeConstants.*;

/**
 * 投稿 Service 实现类
 *
 * @author 泉信管理员
 */
@Service
@Validated
public class SubmitActivityServiceImpl implements SubmitActivityService {

    @Resource
    private SubmitActivityMapper submitActivityMapper;
    @Resource
    private WorkMapper workMapper;
    @Resource
    private ActivityMapper activityMapper;
    @Resource
    private StudentMapper studentMapper;
    @Override
    public Long createSubmitActivity(SubmitActivitySaveReqVO createReqVO) {
        // 插入
        SubmitActivityDO submitActivity = BeanUtils.toBean(createReqVO, SubmitActivityDO.class);
        submitActivityMapper.insert(submitActivity);
        // 返回
        return submitActivity.getId();
    }

    @Override
    public void updateSubmitActivity(SubmitActivitySaveReqVO updateReqVO) {
        // 校验存在
        validateSubmitActivityExists(updateReqVO.getId());
        // 更新
        SubmitActivityDO updateObj = BeanUtils.toBean(updateReqVO, SubmitActivityDO.class);
        submitActivityMapper.updateById(updateObj);
    }

    @Override
    public void deleteSubmitActivity(Long id) {
        // 校验存在
        validateSubmitActivityExists(id);
        // 删除
        submitActivityMapper.deleteById(id);
    }

    private void validateSubmitActivityExists(Long id) {
        if (submitActivityMapper.selectById(id) == null) {
            throw exception(SUBMIT_ACTIVITY_NOT_EXISTS);
        }
    }

    @Override
    public SubmitActivityDO getSubmitActivity(Long id) {
        return submitActivityMapper.selectById(id);
    }

    @Override
    public PageResult<SubmitActivityDO> getSubmitActivityPage(SubmitActivityPageReqVO pageReqVO) {
        return submitActivityMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<SubmitActivityPagesRespVO> getSubmitActivityPageWithName(SubmitActivityPageReqVO pageReqVO) {
        SubmitActivityPageReqVO reqVO= new SubmitActivityPageReqVO();
        BeanUtils.copyProperties(pageReqVO, reqVO);
        return submitActivityMapper.selectPageWithName(pageReqVO,reqVO);
    }

    @Override
    public SubmitActivitysRespVO getSubmitActivityWithName(Long id) {
        //1.查询td_submit_activity表数据
        SubmitActivityDO submitActivityDO = submitActivityMapper.selectById(id);
        //2.查询td_work表数据
        WorkDO workDO = workMapper.selectById(submitActivityDO.getWorkId());
        //3.查询td_activity表数据
        ActivityDO activityDO = activityMapper.selectById(submitActivityDO.getActivityId());
        //TODO 4.查询td_student表数据
        StudentDO studentDO = studentMapper.selectById(submitActivityDO.getStudentId());
        return convertToVO(submitActivityDO,workDO,activityDO,studentDO);
    }
    private SubmitActivitysRespVO  convertToVO(SubmitActivityDO submitActivityDO,WorkDO workDO,ActivityDO activityDO,StudentDO studentDO){
        SubmitActivitysRespVO submitActivitysRespVO = new SubmitActivitysRespVO();
        BeanUtils.copyProperties(submitActivityDO, submitActivitysRespVO); //对象属性拷贝
        submitActivitysRespVO.setWorkName(workDO.getWorkName()); //设置作品名称
        submitActivitysRespVO.setActivityName(activityDO.getActivityName()); //设置活动名称
        //TODO 设置学生姓名
        if(studentDO==null){
            submitActivitysRespVO.setStudentName("张三");
        }else{
            submitActivitysRespVO.setStudentName(studentDO.getRealName());
        }

        return submitActivitysRespVO;
    }

}